class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) 
    {
        sort(nums.begin(),nums.end());
        vector<vector<int>> ans;
        for(int i=0;i<nums.size()-1;i++)
        {
            if(nums[i]>0)
            {
                break;
            }
            if(i>0&&nums[i]==nums[i-1])
            {
                continue;
            }
            int left=i+1;
            int right=nums.size()-1;
            int sum=-1*nums[i];
            while(left<right)
            {
                
                if(nums[left]+nums[right]>sum)
                {
                    right--;
                }
                else if(nums[left]+nums[right]<sum)
                {
                    left++;
                }
                else
                {
                    ans.push_back(vector<int>{nums[i],
                    nums[left],nums[right]});
                    while(left<right&&nums[left]==nums[left+1])
                    {
                        left++;
                    }
                    while(left<right&&nums[right]==nums[right-1])
                    {
                        right--;
                    }
                    left++;
                    right--;
                }
            }
        }
        return ans;

    }
};
